package com.thinksns.sociax.t4.android.gift;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.listener.OnTouchListListener;
import com.thinksns.sociax.t4.adapter.AdapterMyScore;
import com.thinksns.sociax.t4.adapter.AdapterSociaxList;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.component.ListScore;
import com.thinksns.sociax.t4.component.ListSociax;
import com.thinksns.sociax.t4.component.SmallDialog;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.t4.unit.ColorPhrase;
import com.thinksns.sociax.thinksnsbase.bean.ListData;
import com.thinksns.sociax.thinksnsbase.bean.SociaxItem;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient;

import java.util.List;

import static com.thinksns.sociax.t4.android.gift.GoldWithDrawActivity.BUNDLE_ACCOUNT;
import static com.thinksns.sociax.t4.android.gift.GoldWithDrawActivity.BUNDLE_ACCOUNT_TYPE;
import static com.thinksns.sociax.t4.android.gift.GoldWithDrawActivity.BUNDLE_SCORE;

/**
 * 类说明：我的积分
 *
 * @author Zoey
 * @version 1.0
 * @date 2015年9月21日
 */
public class ActivityMyScore extends ThinksnsAbscractActivity {

    private ListSociax listView;
    private ListData<SociaxItem> list;
    private AdapterSociaxList adapter;
    private RelativeLayout rl_score_detail;
    private TextView tv_my_score, tv_score_rule, tv_bind_describe, tv_top_up, tv_transfer, tv_withdraw, tv_exchange_now;
    private ImageButton tv_title_left;
    private ModelUser my;
    private SmallDialog loadDialog;
    public static final int SCORE_TYPE_NONE = 0;
    public static final int SCORE_TYPE_ALIPAY = 1;
    public static final int SCORE_TYPE_WICHAT = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreateNoTitle(savedInstanceState);

        initIntentData();
        initView();
        initListener();
        initData();
    }

    private void initIntentData() {

    }

    @Override
    public OnTouchListListener getListView() {
        return listView;
    }

    private void initView() {

        rl_score_detail = (RelativeLayout) findViewById(R.id.rl_score_detail);
        tv_my_score = (TextView) findViewById(R.id.tv_my_score);
        tv_score_rule = (TextView) findViewById(R.id.tv_score_rule);
        tv_title_left = (ImageButton) findViewById(R.id.tv_title_left);
        tv_exchange_now = (TextView) findViewById(R.id.tv_exchange_now);
        tv_top_up = (TextView) findViewById(R.id.tv_top_up);
        tv_transfer = (TextView) findViewById(R.id.tv_transfer);
        tv_bind_describe = (TextView) findViewById(R.id.tv_bind_describe);
        tv_withdraw = (TextView) findViewById(R.id.tv_withdraw);

        listView = (ListScore) findViewById(R.id.lv_my_score_detail);
        list = new ListData<SociaxItem>();
        adapter = new AdapterMyScore(this, list, 4);
        listView.setAdapter(adapter);
        adapter.loadInitData();


        IntentFilter filter_del_room = new IntentFilter();
        filter_del_room.addAction(StaticInApp.UPDATE_SCORE_DETAIL);
        this.registerReceiver(broad_update_score, filter_del_room);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMyScoreTask();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(broad_update_score);
        super.onDestroy();
    }

    protected BroadcastReceiver broad_update_score = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(StaticInApp.UPDATE_SCORE_DETAIL)) {
                adapter.doRefreshHeader();
              //  getMyScoreTask(); onResume已经调用了一次了
            }
        }
    };

    private void initListener() {
        rl_score_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMyScore.this, ActivityScoreDetail.class);
                startActivity(intent);
            }
        });
        tv_score_rule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMyScore.this, ActivityScoreRule.class);
                startActivity(intent);
            }
        });
        tv_exchange_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMyScore.this, ActivityScoreShop.class);
                startActivity(intent);
            }
        });
        tv_top_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMyScore.this, ActivityScoreTopUp.class);
                startActivity(intent);
            }
        });
        tv_transfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMyScore.this, ActivityScoreTransfer.class);
                startActivity(intent);
            }
        });
        tv_title_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //绑定提现帐号
        tv_bind_describe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (my == null)
                    return;
                toBindAccount();

            }
        });
        //提现
        tv_withdraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (my == null)
                    return;
                if (my.getAccount_type() == SCORE_TYPE_NONE) {
                    toBindAccount();
                } else {
                    Intent intent = new Intent(ActivityMyScore.this, GoldWithDrawActivity.class);
                    intent.putExtra(BUNDLE_SCORE, my.getUserCredit().getScore_value());
                    intent.putExtra(BUNDLE_ACCOUNT, my.getAccount());
                    intent.putExtra(BUNDLE_ACCOUNT_TYPE, my.getAccount_type());
                    startActivity(intent);
                }
            }
        });
    }

    private void toBindAccount() {
        Intent intent = new Intent(ActivityMyScore.this, BindAccountActivity.class);
        intent.putExtra("weichat", my.getAccount_type() == SCORE_TYPE_WICHAT);
        intent.putExtra("alipay", my.getAccount_type() == SCORE_TYPE_ALIPAY);
        startActivity(intent);
    }

    private void initData() {
        loadDialog = new SmallDialog(this, getString(R.string.loading));
        loadDialog.setCancelable(false);
    }

    @Override
    public String getTitleCenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_score;
    }

    private void getMyScoreTask() {
        loadDialog.show();
        Thinksns.getApplication().getUsers().show(Thinksns.getMy(), new ApiHttpClient.HttpResponseListener() {
                    @Override
                    public void onSuccess(Object result) {
                        loadDialog.dismiss();
                        my = ((List<ModelUser>) result).get(0);
                        tv_my_score.setText(my.getUserCredit().getScore_value());
                        String content = "";
                        switch (my.getAccount_type()) {
                            case SCORE_TYPE_NONE:
                                content = getString(R.string.str_bindaccout);
                                break;
                            case SCORE_TYPE_ALIPAY:
                                content = String.format(getString(R.string.str_bind_account_show)
                                        , "支付宝" + my.getAccount());
                                break;
                            case SCORE_TYPE_WICHAT:
                                content = String.format(getString(R.string.str_bind_account_show)
                                        , "微信" + my.getAccount());
                                break;
                        }
                        CharSequence chars = ColorPhrase.from(content)
                                .withSeparator("{}").innerColor(0xffffffff).outerColor(0xffb0e0fb).format();
                        tv_bind_describe.setText(chars);
                    }

                    @Override
                    public void onError(Object result) {
                        loadDialog.dismiss();
                        Toast.makeText(ActivityMyScore.this, getString(R.string.net_work_error), Toast.LENGTH_SHORT).show();
                    }
                }
        );

    }

}
